import { CoolController, BaseController } from '@cool-midway/core';
import { TaskRecordEntity } from '../../entity/record';
import { TaskJobEntity } from '../../entity/job';
import { TaskAppEntity } from '../../entity/app';

/**
 * 任务记录
 */
@CoolController({
  api: ['info', 'page'],
  entity: TaskRecordEntity,
  pageQueryOp: {
    keyWordLikeFields: ['b.name', 'c.name'],
    fieldEq: [
      { requestParam: 'jobId', column: 'a.jobId' },
      { requestParam: 'appId', column: 'a.appId' },
      { requestParam: 'status', column: 'a.status' },
    ],
    select: ['a.*', 'b.name as jobName', 'c.name as appName'],
    join: [
      {
        entity: TaskJobEntity,
        alias: 'b',
        condition: 'a.jobId = b.id',
      },
      {
        entity: TaskAppEntity,
        alias: 'c',
        condition: 'b.appId = c.id',
      },
    ],
    where: async ctx => {
      const { appName, name, topic } = ctx.request.body;
      return [
        ['c.name like :appName', { appName: `%${appName}%` }, appName],
        ['b.name like :name', { name: `%${name}%` }, name],
        ['b.topic like :topic', { topic: `%${topic}%` }, topic],
      ];
    },
  },
})
export class AdminTaskRecordController extends BaseController {}
